<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ConfigBundle
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        if(!Cache::has('web_site')){
            $configs =  \App\Models\Config::select('name','value')->get()->toArray();
            Cache::put('web_site',$configs,24 * 60 * 60);
        }else {
            $configs = Cache::get('web_site');
            //将系统配置写入配置树
            foreach ($configs as &$item) {
                config(['web_site.'.$item['name']=>$item['value']]);
            }
        }
        return $next($request);
    }
}
